package com.huadao.cart_ccs.experiment.log;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioGroup;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseDialogFragment;
import com.huadao.cart_ccs.base.BasePresenter;
import com.huadao.cart_ccs.experiment.log.environmentLog.EnvironmentLogFrag;
import com.huadao.cart_ccs.experiment.log.exception.ExceptionFrag;
import com.huadao.cart_ccs.experiment.log.operationLog.HumanOperateLogFrag;
import com.huadao.cart_ccs.experiment.log.operationRecord.OperationRecordFrag;
import com.huadao.cart_ccs.experiment.log.paramsAnysis.CO2Frag;
import com.huadao.cart_ccs.experiment.log.paramsAnysis.O2Frag;
import com.huadao.cart_ccs.experiment.log.paramsAnysis.TemperatureFrag;
import com.huadao.cart_ccs.experiment.log.protocolLog.ProtocolLogFrag;
import com.huadao.cart_ccs.common.ProjectManager;
import com.huadao.cart_ccs.common.adapter.CommonRecycleAdapter;
import com.huadao.cart_ccs.common.adapter.CommonViewHolder;
import com.huadao.cart_ccs.common.adapter.FooterHolder;
import com.huadao.cart_ccs.common.adapter.HeaderHolder;
import com.huadao.cart_ccs.common.adapter.onItemCommonClickListener;
import com.huadao.cart_ccs.data.bo.ExperimentStage;
import com.huadao.cart_ccs.greendao.GreenDaoManager;
import com.huadao.cart_ccs.greendao.gen.ExperimentStageDao;

import java.util.List;

import butterknife.OnClick;

public class LogDF extends BaseDialogFragment {

    private FragmentManager fragmentManager;
    private ExceptionFrag exceptionFrag;
    private TemperatureFrag temperatureFrag;
    private CO2Frag co2Frag;
    private O2Frag o2Frag;
    private ProtocolLogFrag protocolLogFrag;
    private OnDay currentFrag;
    private HumanOperateLogFrag humanOperateLogFrag;
    private EnvironmentLogFrag environmentLogFrag;
    private OperationRecordFrag operationRecordFrag;

//    private Spinner sp_days;
    private int selectedDay;
    private List<ExperimentStage> experimentStageList;

    private RadioGroup bg_tab;
    private RecyclerView rv_days;
    private DayAdapter adapter;

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.frag_logexception;
    }

    @Override
    protected void onInit() {
        setSetIconsVisibility(View.INVISIBLE);
        setTitle("日志");
        fragmentManager = getChildFragmentManager();
        long projectId= ProjectManager.getInstance().getProjectId();
        experimentStageList= GreenDaoManager.getInstance().getNoCacheSession().getExperimentStageDao().queryBuilder().where(ExperimentStageDao.Properties.ProjectId.eq(projectId)).orderAsc(ExperimentStageDao.Properties.StartDate).list();

        bg_tab=view.findViewById(R.id.bg_tab);
        bg_tab.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==R.id.rb_operate_record){
                    if(operationRecordFrag==null){
                        operationRecordFrag=OperationRecordFrag.getInstance(selectedDay);
                    }else {
                        operationRecordFrag.setDayBundle(selectedDay);
                    }
                    replaceFrag(operationRecordFrag);
                }else if(checkedId==R.id.rb_exception){
                    if (exceptionFrag == null) {
                        exceptionFrag = ExceptionFrag.getInstance(selectedDay);
                    }else {
                        exceptionFrag.setDayBundle(selectedDay);
                    }
                    replaceFrag(exceptionFrag);
                }
            }
        });
        rv_days=view.findViewById(R.id.rv_days);
        adapter=new DayAdapter(getActivity(),experimentStageList,R.layout.item_day);
        rv_days.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_days.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
        rv_days.setAdapter(adapter);

        //默认当前阶段
        selectedDay=experimentStageList.get(experimentStageList.size()-1).getDay();
        if(currentFrag!=null){
            currentFrag.daySelected(selectedDay);
        }
        adapter.notifyDataSetChanged();

        bg_tab.check(R.id.rb_operate_record);
    }

    private void replaceFrag(Fragment fragment) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fl_log_exception, fragment).commitAllowingStateLoss();
        currentFrag= (OnDay) fragment;
    }

    class DayAdapter extends CommonRecycleAdapter<ExperimentStage>{

//        private onItemCommonClickListener onItemCommonClickListener=new onItemCommonClickListener() {
//            @Override
//            public void onItemClickListener(int position) {
//                if (selectedDay != position + 1) {
//                    selectedDay=position+1;
//                    if(currentFrag!=null){
//                        currentFrag.daySelected(selectedDay);
//                    }
//                    notifyDataSetChanged();
//                }
//            }
//
//            @Override
//            public void onItemLongClickListener(int position) {
//
//            }
//        };

        public DayAdapter(Context context, List<ExperimentStage> dataList, int layoutId) {
            super(context, dataList, layoutId);
        }

        @Override
        protected void bindData(CommonViewHolder holder, final ExperimentStage data) {
            holder.setText(R.id.check_day,getString(R.string.day)+data.getDay());
            holder.setCheckedChangeListener(R.id.check_day,null);
            if(selectedDay==data.getDay()){
                holder.setChecked(R.id.check_day,true);
            }else {
                holder.setChecked(R.id.check_day,false);
            }
//            if(selectedDay==data.getDay()){
//                holder.setBackgroung(R.id.btn_day,R.color.blue);
//                holder.setTextColor(R.id.btn_day,getResources().getColor(R.color.white));
//            }else {
//                holder.setBackgroung(R.id.btn_day,R.color.WHITE);
//                holder.setTextColor(R.id.btn_day,getResources().getColor(R.color.blue));
//            }
//            holder.setCommonClickListener(onItemCommonClickListener);

            holder.setCheckedChangeListener(R.id.check_day, new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(selectedDay!=data.getDay()) {
                        int oldDay=selectedDay;
                        selectedDay = data.getDay();
                        notifyDataSetChanged();
//                        notifyItemChanged(oldDay-1);
//                        notifyItemChanged(selectedDay-1);
                        if(currentFrag!=null){
                            currentFrag.daySelected(selectedDay);
                        }
                    }

                }
            });
        }

        @Override
        protected void bindFooterData(FooterHolder holder) {

        }

        @Override
        protected void bindHeaderData(HeaderHolder holder) {

        }
    }
}
